package cn.edu.xmu.oomall.liquidation.model.bo.liquid.util;

import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.liquidation.dao.LiquidationDao;
import cn.edu.xmu.oomall.liquidation.microservice.GoodService;
import cn.edu.xmu.oomall.liquidation.microservice.PaymentService;
import cn.edu.xmu.oomall.liquidation.microservice.ShopService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.PaymentRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.RefundRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.SimpleShopVo;
import cn.edu.xmu.oomall.liquidation.model.bo.ExpenditureItem;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.bo.RevenueItem;
import cn.edu.xmu.oomall.liquidation.model.po.RevenueItemPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;

@Component
public class LiquidUtil {
    @Autowired
    private GoodService goodService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private LiquidationDao liquidationDao;

    private List<String> platformCalcFieldList = List.of("expressFee", "commission");

    private List<String> shopCalcFieldList = List.of("expressFee", "commission", "shopRevenue", "point");

    private static Long userId = 1L;

    private static String userName = "admin";

    // 根据shopId和清算日期获得清算单
    public Liquidation getLiquid(Long shopId, LocalDateTime liquidDateTime) {
        ReturnObject returnObject = liquidationDao.getLiquidation(shopId, liquidDateTime);
        Liquidation liquidation;
        if (returnObject.getData() == null) {
            SimpleShopVo shopVo = shopService.getShopsById(shopId).getData();
            liquidation = new Liquidation();
            liquidation.setShopId(shopId);
            liquidation.setShopName(shopVo.getName());
            liquidation.setLiquidDate(liquidDateTime);
            liquidation.setExpressFee(0L);
            liquidation.setCommission(0L);
            liquidation.setShopRevenue(0L);
            liquidation.setPoint(0L);
            liquidation.setState(Liquidation.LiquidationState.NOT_REMITTED);
            ReturnObject returnObject1 = liquidationDao.insertLiquidation(liquidation);
            liquidation = (Liquidation) returnObject1.getData();
        }
        else {
            liquidation = (Liquidation) returnObject.getData();
        }

        return liquidation;
    }

    /**
     * 通过productId 获得productName
     * @param productId
     * @return
     */
    public String getProductNameById(Long productId) {
        return goodService.getProductById(productId).getData().getName();
    }

    public void setCreator(Long userId, String userName) {
        LiquidUtil.userId = userId;
        LiquidUtil.userName = userName;
    }

    public static Long getUserId() {
        return userId;
    }

    public static String getUserName() {
        return userName;
    }

    public List<String> getPlatformCalcFieldList() {
        return platformCalcFieldList;
    }

    public List<String> getShopCalcFieldList() {
        return shopCalcFieldList;
    }

    public ReturnObject insertRevenue(RevenueItem revenueItem) {
        return liquidationDao.insertRevenue(revenueItem);
    }

    public ReturnObject updateLiquid(Liquidation liquid) {
        return liquidationDao.updateLiquid(liquid);
    }

    public ReturnObject getRevenueItem(RevenueItemPoExample revenueItemPoExample) {
        return liquidationDao.getRevenueItem(revenueItemPoExample);
    }

    public ReturnObject insertExpenditure(ExpenditureItem expenditureItem) {
        return liquidationDao.insertExpenditure(expenditureItem);
    }

    public ReturnObject insertLiquidation(Liquidation platformLiquidation) {
        return liquidationDao.insertLiquidation(platformLiquidation);
    }

    public InternalReturnObject changePaymentLiquidated(Long paymentId) {
        return paymentService.changePaymentLiquidated(paymentId);
    }

    public InternalReturnObject changeRefundLiquidated(Long refundId) {
        return paymentService.changeRefundLiquidated(refundId);
    }

    public InternalReturnObject<PageInfo<PaymentRetVo>> getPaymentByTime(ZonedDateTime beginTime, ZonedDateTime endTime, Integer page, Integer pageSize) {
        return paymentService.getPaymentByStateAndTime(0L, 2, beginTime, endTime, page, pageSize);
    }

    public InternalReturnObject<PageInfo<RefundRetVo>> getRefundByTime(ZonedDateTime beginTime, ZonedDateTime endTime, Integer page, Integer pageSize) {
        return paymentService.getRefundByStateAndTime(0L, 2, beginTime, endTime, page, pageSize);
    }
}
